package com.restaurant.springboot.controller;

import com.restaurant.springboot.annotation.TokenAuth;
import com.restaurant.springboot.entity.OrderEntity;
import com.restaurant.springboot.entity.vo.OrderItemVO;
import com.restaurant.springboot.entity.vo.OrderVO;
import com.restaurant.springboot.entity.vo.PageEntity;
import com.restaurant.springboot.service.DishesService;
import com.restaurant.springboot.service.OrderService;
import com.restaurant.springboot.utils.RespCode;
import com.restaurant.springboot.utils.RespEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author jack
 * @Date:2019/7/31 9:39
 */
@RestController
@RequestMapping("/api/admin/order")
public class AdminOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    DishesService dishesService;

    @GetMapping("")
    public RespEntity getAllOrder(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")int pageSize,@RequestParam(value = "datePickerStart",required = false)String startTime,@RequestParam(value = "datePickerEnd",required = false)String endTime) throws ParseException {
        Page<OrderEntity> allOrder;
        if(startTime == null || endTime == null || "".equals(startTime) || "".equals(endTime)){
            allOrder = orderService.getAllOrder(pageNum, pageSize);
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date start = sdf.parse(startTime);
            Date end = sdf.parse(endTime);
            allOrder = orderService.getAllOrder(pageNum, pageSize, start, end);
        }
        int total = allOrder.getTotalPages();
        List<OrderEntity> content = allOrder.getContent();
        List viewOrder = new ArrayList();
        content.forEach((e)->{
            OrderVO orderVO = new OrderVO();
            List<OrderItemVO> dishes = dishesService.getAllOrderItemByOrderId(e.getId());
            List<String> list = new ArrayList<>();
            dishes.forEach((y)->{
                list.add(y.getName() + " X " + y.getNumber());
            });
            orderVO.setList(list);
            orderVO.setOrderEntity(e);
            viewOrder.add(orderVO);
        });
        PageEntity page = new PageEntity();
        page.setContent(viewOrder);
        page.setTotalPage(total);
        page.setTotal(allOrder.getTotalElements());
        return new RespEntity(
                RespCode.SUCCESS,
                page
        );
    }

        /**
     * 修改订单状态
     * @param state 表示要设置为什么状态
     * @param id 订单的id
     * @return 处理结果
     *
     */
    @PutMapping("/{id}")
    @TokenAuth
    public RespEntity changeOrder(@PathVariable int id,@RequestParam("state")String state){
        String stateValue = state.toLowerCase();
        OrderEntity orderById = orderService.getOrderById(id);
        if(orderById ==null){
            return new RespEntity(
                    500,
                    "找不到订单"
            );
        }
        boolean result = isResult(stateValue, orderById, orderService);
        if(result){
            return new RespEntity(
                    200
                    ,"ok"
            );
        }else{
            return new RespEntity(
                    RespCode.ERROR
            );
        }
    }

    public static boolean isResult(String stateValue, OrderEntity orderById, OrderService orderService) {
        boolean result = false;
        switch (stateValue) {
            case "handling":
                result = orderService.handlingOrder(orderById);
                break;
            case "cancel":
                result=  orderService.cancelOrder(orderById);
                break;
            case "finished":
                result = orderService.finishOrder(orderById);
        }
        return result;
    }
}
